from typing import Any

from vnpy.trader.object import BarData, TickData
from vnpy.trader.utility import BarGenerator, ArrayManager

from vnpy_ctastrategy.template import (
    CtaTemplate
)


class MonitorStrategy(CtaTemplate):
    """"""

    author: str = "Cjie Wong"
    # 上突破价格
    up_price = 1000

    cur_price: float = 0.0

    parameters: list = [
        "up_price",
    ]

    variables: list = [
        "cur_price",
    ]

    def __init__(
            self,
            cta_engine: Any,
            strategy_name: str,
            vt_symbol: str,
            setting: dict,
    ) -> None:
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)
        self.bg = BarGenerator(self.on_bar)
        self.am = ArrayManager()

    def on_init(self) -> None:
        """策略初始化"""
        self.write_log(f"策略[{self.strategy_name}]初始化")
        self.push_msg(f"策略[{self.strategy_name}]已停止")
        self.load_bar(10)

    def on_start(self) -> None:
        """启动"""
        self.write_log("策略启动")

    def on_stop(self) -> None:
        """停止"""
        self.push_msg(f"策略[{self.strategy_name}]已停止")
        self.write_log("策略停止")

    def on_tick(self, tick: TickData) -> None:
        """TICK更新"""
        self.bg.update_tick(tick)

    def on_bar(self, bar: BarData) -> None:
        """k线更新"""
        am = self.am

        am.update_bar(bar)
        if not am.inited:
            return
        self.cur_price = bar.close_price
        if self.cur_price >= self.up_price:
            time: str = bar.datetime.strftime("%H:%M:%S\t")
            msg: str = f"突破设置的上限价格，up_price={self.up_price},cur_price={self.cur_price},datetime={time}"
            self.push_msg(msg)
            self.write_log(msg)
            self.send_email(msg)
        # 更新图形界面
        self.put_event()



















































